package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ReturnType;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.WmsLibraryArea;
import com.jy.wms.service.WmsLibraryAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @ClassName: WmsLibraryAreaController
 * @Description: 字库 库区表
 * @Author yjs
 * @Date Tue Aug 29 15:59:22 GMT+08:00 2017
 */
@Controller
@RequestMapping("/wmslibraryarea")
@Api(value = "/wmslibraryarea", description = "关于库区管理操作")
public class  WmsLibraryAreaController extends BaseController<WmsLibraryAreaService,WmsLibraryArea> {
    @ApiOperation(value = "库区管理 POST请求查询仓库信息",response = JSONObject.class, notes = "POST请求查询库区信息")
    @PostMapping(value="/list")
    public @ResponseBody JSONObject getWmsLibraryArea(@RequestBody WmsLibraryArea entity, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckmc= String.valueOf(userinfo.getUser().getOrgName());//仓库名称
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            entity.setCkid(ckid);
            Page<WmsLibraryArea> list= baseService.baseSelectPage(entity);
            for(int i=0;i<list.size();i++){
                list.get(i).setCk_mc(ckmc);
                list.get(i).setCkid(ckid);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;

    }
    @PostMapping(value = "/add")
    @ApiOperation(value = "库区信息 新增库区信息",response = JSONObject.class, notes = "POST新增库区信息")
    public @ResponseBody JSONObject addWmsLibraryArea(@RequestBody WmsLibraryArea entity, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckmc= String.valueOf(userinfo.getUser().getOrgName());//仓库名称
            String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            Integer flag=0;
            WmsLibraryArea entitys=baseService.searchADD(entity.getMc(),ckid,"");
            WmsLibraryArea entitys2=baseService.searchADD("",ckid,entity.getDm());
            if(entitys!=null){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.info.toString(),"新增库区名称重复","新增库区名称重复","");
            }else if(entitys2!=null) {
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.info.toString(),"新增库区代码重复","新增库区代码重复","");
            }else{
                entity.setId(UUIDUtils.uuid());
                entity.setSfyx("1");
                entity.setCk_mc(ckmc);
                entity.setCkid(ckid);
                baseService.baseInsert(entity);
            }
        }catch (SucessException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增库区成功","", JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"新增库区失败",e.getMessage(),"");
        } catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增库区失败","新增库区失败","");
        }
        return json;
    }

    /**
     * 编辑库区信息
     *
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "库区信息 编辑库区信息", response = JSONObject.class, notes = "POST编辑库区信息")
    public @ResponseBody
    JSONObject editWmsLibraryArea(@RequestBody WmsLibraryArea entity, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckmc = String.valueOf(userinfo.getUser().getOrgName());//仓库名称
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            Integer flag = 0;
            WmsLibraryArea entitys = baseService.searchEdit(entity.getMc(), ckid, "", entity.getId());
            WmsLibraryArea entitys2 = baseService.searchEdit("", ckid, entity.getDm(), entity.getId());
            if (entitys != null) {
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.info.toString(), "编辑库区信息重复", "编辑库区名称重复", "");
            } else if (entitys2 != null) {
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.info.toString(), "编辑库区信息重复", "编辑库区代码重复", "");
            } else {
                entity.setCk_mc(ckmc);
                entity.setCkid(ckid);
                baseService.editWmsLibraryArea(entity);
            }
        } catch (SucessException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "编辑库区信息成功", "", JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "编辑库区信息失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "编辑库区信息失败", "编辑库区信息失败", "");
        }
        return json;
    }

    /**
     * 编辑库区信息
     *
     * @param map
     * @return
     */
    @PostMapping(value = "/baseSelectByPrimaryKey")
    @ApiOperation(value = "库区信息 查看库区信息", response = JSONObject.class, notes = "GET查看库区信息")
    public @ResponseBody
    JSONObject baseSelectByPrimaryKey(@RequestBody Map map, HttpSession session) {
        JSONObject json = new JSONObject();
        WmsLibraryArea entity = new WmsLibraryArea();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckmc = String.valueOf(userinfo.getUser().getOrgName());//仓库名称
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            entity = baseService.baseSelectByPrimaryKey(map.get("id").toString());
            entity.setCk_mc(ckmc);
            entity.setCkid(ckid);
            json = JSONObject.fromObject(entity);
        } catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"编辑库区信息失败","编辑库区信息失败","");
        }
        return json;
    }

    /**
     * 库区导入
     *
     * @param session
     * @param filepath
     * @return
     */
    @ApiOperation(value = "库区导入", response = JSONObject.class, notes = "库区导入")
    @PostMapping(value = "importKQ")
    public @ResponseBody
    JSONObject importKQ(HttpSession session, @RequestBody String filepath) {
        try {
            baseService.importKQ(session, filepath);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "库区导入成功", "", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "库区导入失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "库区导入失败", e.getMessage(), "");
        }
    }
}
